<template>
  <div class="login-form-wrapper">
    <div class="login-header">
      <h2 class="welcome">欢迎回来</h2>
      <p class="desc">登录您的账号以继续访问</p>
    </div>

    <div v-if="errorMessage" class="login-form-error">
      <a-alert type="error" :title="errorMessage" closable />
    </div>

    <a-form
      ref="loginForm"
      :model="userInfo"
      class="login-form"
      layout="vertical"
      @submit="handleSubmit"
    >
      <a-form-item
        field="username"
        :rules="[{ required: true, message: $t('login.form.userName.errMsg') }]"
        hide-label
        class="form-item"
      >
        <a-input
          v-model="userInfo.username"
          :placeholder="$t('login.form.userName.placeholder')"
          size="large"
          class="input-field"
        >
          <template #prefix>
            <icon-user class="input-icon" />
          </template>
        </a-input>
      </a-form-item>

      <a-form-item
        field="password"
        :rules="[{ required: true, message: $t('login.form.password.errMsg') }]"
        hide-label
        class="form-item"
      >
        <a-input-password
          v-model="userInfo.password"
          :placeholder="$t('login.form.password.placeholder')"
          size="large"
          allow-clear
          class="input-field"
        >
          <template #prefix>
            <icon-lock class="input-icon" />
          </template>
        </a-input-password>
      </a-form-item>

      <div class="login-form-actions">
        <a-checkbox
          :model-value="loginConfig.rememberPassword"
          class="remember-checkbox"
          @change="setRememberPassword"
        >
          {{ $t('login.form.rememberPassword') }}
        </a-checkbox>
        <a-link class="forgot-link">忘记密码？</a-link>
      </div>

      <a-button
        type="primary"
        html-type="submit"
        long
        size="large"
        :loading="loading"
        class="submit-btn"
      >
        <template v-if="!loading" #icon>
          <icon-login />
        </template>
        {{ $t('login.form.login') }}
      </a-button>

      <!-- 第三方登录 -->
      <div class="other-login">
        <div class="divider">
          <span>或使用以下方式登录</span>
        </div>
        <div class="social-login">
          <a-button
            class="social-btn github-btn"
            @click="handleSocialLogin('github')"
          >
            <template #icon>
              <icon-github />
            </template>
            GitHub
          </a-button>
          <a-button
            class="social-btn google-btn"
            @click="handleSocialLogin('google')"
          >
            <template #icon>
              <icon-google />
            </template>
            Google
          </a-button>
        </div>
      </div>
    </a-form>

    <!-- 快速登录提示 -->
    <div class="demo-tips">
      <a-alert type="info" show-icon>
        <template #title> 演示账号</template>
        账号：admin，密码：123456
      </a-alert>
    </div>
  </div>
</template>

<script lang="ts" setup>
  import { reactive, ref } from 'vue';
  import { useRouter } from 'vue-router';
  import { Message } from '@arco-design/web-vue';
  import { ValidatedError } from '@arco-design/web-vue/es/form/interface';
  import { useI18n } from 'vue-i18n';
  import { useStorage } from '@vueuse/core';
  import { useUserStore } from '@/store';
  import useLoading from '@/hooks/loading';
  import type { LoginData } from '@/api/user/types';

  const router = useRouter();
  const { t } = useI18n();
  const errorMessage = ref('');
  const { loading, setLoading } = useLoading();
  const userStore = useUserStore();

  const loginConfig = useStorage('login-config', {
    rememberPassword: true,
    username: 'test',
    password: '123456',
  });

  const userInfo = reactive({
    username: loginConfig.value.username,
    password: loginConfig.value.password,
  });

  const handleSubmit = async ({
    errors,
    values,
  }: {
    errors: Record<string, ValidatedError> | undefined;
    values: Record<string, any>;
  }) => {
    if (loading.value) return;
    if (!errors) {
      setLoading(true);
      try {
        await userStore.login(values as LoginData);
        const { redirect, ...othersQuery } = router.currentRoute.value.query;
        router.push({
          name: (redirect as string) || 'Workplace',
          query: {
            ...othersQuery,
          },
        });
        Message.success(t('login.form.login.success'));
        const { rememberPassword } = loginConfig.value;
        const { username, password } = values;
        loginConfig.value.username = rememberPassword ? username : '';
        loginConfig.value.password = rememberPassword ? password : '';
      } catch (err) {
        errorMessage.value = (err as Error).message;
      } finally {
        setLoading(false);
      }
    }
  };

  const setRememberPassword = (value: boolean) => {
    loginConfig.value.rememberPassword = value;
  };

  const handleSocialLogin = (platform: string) => {
    Message.info(`${platform} 登录功能开发中...`);
  };
</script>

<style lang="less" scoped>
  .login-form-wrapper {
    width: 100%;
    max-width: 340px;
    margin: 0 auto;

    .login-header {
      text-align: center;
      margin-bottom: 40px;

      .logo {
        width: 48px;
        height: 48px;
        margin-bottom: 16px;
      }

      .welcome {
        margin: 0 0 8px;
        font-size: 28px;
        font-weight: 600;
        color: var(--color-text-1);
      }

      .desc {
        margin: 0;
        font-size: 16px;
        color: var(--color-text-3);
      }
    }

    .login-form-error {
      margin-bottom: 24px;
      animation: shake 0.5s ease-in-out;
    }

    @keyframes shake {
      0%,
      100% {
        transform: translateX(0);
      }
      25% {
        transform: translateX(-5px);
      }
      75% {
        transform: translateX(5px);
      }
    }

    .login-form {
      .form-item {
        margin-bottom: 24px;

        .input-field {
          border-radius: 12px;
          border: 2px solid transparent;
          background: rgba(0, 0, 0, 0.02);
          backdrop-filter: blur(10px);
          transition: all 0.3s ease;

          &:hover {
            background: rgba(0, 0, 0, 0.04);
            border-color: rgba(102, 126, 234, 0.3);
          }

          &:focus,
          &:focus-within {
            background: rgba(255, 255, 255, 0.8);
            border-color: #667eea;
            box-shadow: 0 0 0 4px rgba(102, 126, 234, 0.1);
          }

          .input-icon {
            color: rgba(0, 0, 0, 0.4);
            font-size: 16px;
            transition: color 0.3s ease;
          }

          &:focus-within .input-icon {
            color: #667eea;
          }
        }
      }

      .login-form-actions {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 32px;

        .remember-checkbox {
          color: rgba(0, 0, 0, 0.7);
          font-weight: 500;
        }

        .forgot-link {
          color: #667eea;
          font-weight: 500;
          text-decoration: none;

          &:hover {
            color: #764ba2;
            text-decoration: underline;
          }
        }
      }

      .submit-btn {
        height: 48px;
        border-radius: 12px;
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        border: none;
        font-size: 16px;
        font-weight: 600;
        margin-bottom: 32px;
        transition: all 0.3s ease;
        box-shadow: 0 4px 16px rgba(102, 126, 234, 0.3);

        &:hover {
          transform: translateY(-2px);
          box-shadow: 0 8px 24px rgba(102, 126, 234, 0.4);
        }

        &:active {
          transform: translateY(0);
        }

        &.arco-btn-loading {
          background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
      }

      .other-login {
        .divider {
          position: relative;
          text-align: center;
          margin: 32px 0 24px;

          &::before {
            content: '';
            position: absolute;
            top: 50%;
            left: 0;
            right: 0;
            height: 1px;
            background: linear-gradient(
              90deg,
              transparent 0%,
              rgba(0, 0, 0, 0.1) 50%,
              transparent 100%
            );
          }

          span {
            position: relative;
            padding: 0 20px;
            background: #f8f7fc;
            color: rgba(0, 0, 0, 0.5);
            font-size: 14px;
            font-weight: 500;
            z-index: 1;
          }
        }

        .social-login {
          display: flex;
          gap: 12px;

          .social-btn {
            flex: 1;
            height: 44px;
            border-radius: 10px;
            font-weight: 500;
            transition: all 0.3s ease;

            &.github-btn {
              background: #24292e;
              color: white;
              border-color: #24292e;

              &:hover {
                background: #1a1e22;
                transform: translateY(-1px);
                box-shadow: 0 4px 12px rgba(36, 41, 46, 0.3);
              }
            }

            &.google-btn {
              background: #4285f4;
              color: white;
              border-color: #4285f4;

              &:hover {
                background: #357ae8;
                transform: translateY(-1px);
                box-shadow: 0 4px 12px rgba(66, 133, 244, 0.3);
              }
            }
          }
        }
      }
    }

    .demo-tips {
      margin-top: 24px;

      :deep(.arco-alert) {
        border-radius: 12px;
        background: rgba(24, 144, 255, 0.05);
        border-color: rgba(24, 144, 255, 0.2);
      }
    }
  }

  // 响应式设计
  @media (max-width: 480px) {
    .login-form-wrapper {
      .login-form {
        .other-login .social-login {
          flex-direction: column;
        }
      }
    }
  }
</style>
